package cn.iot.controller;

import cn.iot.common.api.CommonResult;
import cn.iot.domain.Floor;
import cn.iot.domain.Loudong;
import cn.iot.domain.Room;
import cn.iot.service.FloorService;
import cn.iot.service.LoudongService;
import cn.iot.service.LoudongTypeService;
import cn.iot.service.RoomService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.models.auth.In;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("loudong")
public class LoudongController {
    @Autowired
    LoudongService lendongService;
    @Autowired
    LoudongTypeService lendongTypeService;
    @PostMapping("/getType")
    public CommonResult getTYpe(){
        return CommonResult.success(lendongTypeService.list());
    }
    @PostMapping("/getPage")
    public CommonResult getPage(Integer pageNum, Integer pageSize, @RequestBody Loudong loudong) {
        pageSize=99999999;
        Page<Loudong> page=new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<Loudong> query = new LambdaQueryWrapper<>();
        query.like(!StringUtils.isEmpty(loudong.getLoudongName()),Loudong::getLoudongName,loudong.getLoudongName());
        query.eq(loudong.getType()!=null,Loudong::getType,loudong.getType());
        lendongService.page(page,query);
        page.getRecords().stream().map(ld -> {
            ld.setTypeName(lendongTypeService.getById(ld.getType()).getName());
            return ld;
        }).collect(Collectors.toList());
        return CommonResult.success(page);
    }
    @Autowired
    FloorService floorService;
    @PostMapping("/saveOrUpdate")
    public CommonResult saveOrUpdate(@RequestBody Loudong loudong){
        Integer id = loudong.getId();
        Loudong l=new Loudong();
        if(id!=null){
            l = lendongService.getById(id);
        }
        lendongService.saveOrUpdate(loudong);

        if (id==null&&loudong.getLoudongNum()!=null||l.getLoudongNum()!=loudong.getLoudongNum()) {
            for (int i = 1; i <= loudong.getLoudongNum(); i++) {
                Floor floor = new Floor();
                floor.setLoudongId(loudong.getId());
                floor.setFloorId(i);
                floor.setFloorName(i+"楼");
                floorService.saveOrUpdate(floor);
            }
        }
        return CommonResult.success();
    }
    @Autowired
    RoomService roomService;
    @DeleteMapping("/del/{id}")
    public CommonResult deleteById(@PathVariable Integer id){
        LambdaQueryWrapper<Floor> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Floor::getLoudongId,id);
        List<Floor> floors = floorService.list(queryWrapper);
        for (Floor floor : floors) {
            LambdaQueryWrapper<Room> wrapper=new LambdaQueryWrapper<>();
            wrapper.eq(Room::getFloorId,floor.getId());
            List<Room> rooms = roomService.list(wrapper);
            for (Room room : rooms) {
                roomService.removeById(room.getId());
            }
            floorService.removeById(floor.getId());
        }
        lendongService.removeById(id);
        return CommonResult.success();
    }
}
